/*
 *  txdnet.cn tonydon
 * 
 */
package cn.txdnet.txcms.auth;

import cn.txdnet.txweb.utils.JsonUtil;
import cn.txdnet.txweb.utils.StringUtil;
import cn.txdnet.txweb.utils.WebUtil;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import txdnet.util.Logger;
import txdnet.util.StaticMark;

/**
 *
 * @author txdnet
 */
public class AdminRoleAccessConfig {

    private static Class clazz = AdminRoleAccessConfig.class;
    private static Map<String, AdminRoleAccessBean> accessPathMapper = new HashMap<String, AdminRoleAccessBean>();

    /**
     * 初始化管理员角色访问设置
     * @param p 
     */
    public static void init(String roleConfFile) {
        if (StringUtil.isEmpty(roleConfFile)) {
            accessPathMapper = Collections.emptyMap();
            Logger.warn(clazz, "Admin Role Access Not Config!");
            return;
        }
        List<AdminRoleAccessBean> list = loadConf(roleConfFile);
        if (list == null) {
            accessPathMapper = Collections.emptyMap();
            Logger.warn(clazz, "Admin Role Access Config Is Empty!");
            return;
        }
        for (AdminRoleAccessBean bean : list) {
            accessPathMapper.put(bean.getPath(), bean);
        }
    }

    /**
     * 导入配置文件生成list对象
     *
     * @param file
     * @return
     */
    private static List<AdminRoleAccessBean> loadConf(String file) {
        String path_conf;
        List<AdminRoleAccessBean> list = null;
        try {
            path_conf = IOUtils.toString(clazz.getResourceAsStream(StaticMark.STR_SLASH.concat(file))).trim();
            // 空配置文件跳过
            // 去掉注释:
            path_conf = WebUtil.clearNote(path_conf);
            // JSON配置转为LIST<BEAN>
            list = JsonUtil.parseArray(path_conf, AdminRoleAccessBean.class);
            Logger.info(clazz, "conf = ".concat(file));
        } catch (Exception e) {
            Logger.error(clazz, e.toString().concat(".loadConf()"));
        }
        return list;
    }
    
    /**
     * 获取角色进入定义BEAN
     * @param urlaction
     * @return 
     */
    public static AdminRoleAccessBean getRoleAccessBean(String urlaction){
        return accessPathMapper.get(urlaction);
    }
    
    /**
     * 清空配置
     */
    public static void destroy(){
        accessPathMapper.clear();
        accessPathMapper = null;
    }
}
